package com.alisiwei.core.base;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;

public class BaseController {
	
	private static Logger logger = Logger.getLogger(BaseController.class);
	
	/**
	 * 描述：向页面打印json数据
	 * 作者：zhangkai
	 * @param object
	 * @param httpServletResponse
	 */
	public void OutputJson(Object object, HttpServletResponse httpServletResponse) {
		PrintWriter out = null;
		httpServletResponse.setContentType("application/json");
		httpServletResponse.setCharacterEncoding("utf-8");
		String json = null;
		try {
			out = httpServletResponse.getWriter();
			json = JSON.toJSONStringWithDateFormat(object,"yyyy-MM-dd HH:mm:ss");
			logger.info("输出的json：" + json);
			out.print(json);
			out.close();
		} catch (IOException e) {
			logger.error("输出异常");
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述：向页面打印数据
	 * 作者：zhangkai
	 * @param message
	 * @param httpServletResponse
	 */
	public void OutputHtml(String message, HttpServletResponse httpServletResponse) {
		PrintWriter out = null;
		httpServletResponse.setContentType("application/html");
		httpServletResponse.setCharacterEncoding("utf-8");
		try {
			out = httpServletResponse.getWriter();
			logger.info("输出的message：" + message);
			out.print(message);
			out.close();
		} catch (IOException e) {
			logger.error("输出异常");
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述：设置页面参数
	 * 作者：zhangkai
	 * @param map
	 */
	public void setAtribute(Map<String, Object> map,HttpServletRequest request){
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			request.setAttribute(entry.getKey(), entry.getValue());
		}
	}
	
	/**
	 * 描述：设置cookie
	 * 作者：zhangkai
	 * @param name
	 * @param value
	 * @param response
	 */
	public void addLoginCookie(String name, String value, HttpServletResponse response){
		logger.info("添加cookie:name="+name+";value="+value+";");
		Cookie cookie = new Cookie(name.trim(), value.trim());
        cookie.setMaxAge(30 * 60);// 设置为30min
        cookie.setPath("/");
        response.addCookie(cookie);
	}
	
	/**
	 * 描述：删除cookie
	 * 作者：zhangkai
	 * @param name
	 * @param request
	 * @param response
	 */
	public void removeLoginCookie(String name, HttpServletRequest request, HttpServletResponse response){
		logger.info("删除cookie:name="+name+";");
		Cookie[] cookies = request.getCookies();
        if (null==cookies) {
        	logger.error("删除cookie:没有cookie==============");
        } else {
            for(Cookie cookie : cookies){
                if(cookie.getName().equals(name)){
                    cookie.setValue(null);
                    cookie.setMaxAge(0);// 立即销毁cookie
                    cookie.setPath("/");
                    logger.info("被删除的cookie名字为:"+cookie.getName());
                    response.addCookie(cookie);
                    break;
                }
            }
        }
	}
	
	/**
	 * 根据名字获取cookie
	 * 作者：zhangkai
	 * @param request
	 * @param name cookie名字
	 * @return
	 */
	public Cookie getCookieByName(String name, HttpServletRequest request){
	    Map<String,Cookie> cookieMap = ReadCookieMap(request);
	    if(cookieMap.containsKey(name)){
	        Cookie cookie = (Cookie)cookieMap.get(name);
	        logger.info("cookie名字为:"+cookie.getName()+",value:"+cookie.getValue());
	        return cookie;
	    }else{
	        return null;
	    }   
	}
	 
	 
	 
	/**
	 * 将cookie封装到Map里面
	 * 作者：zhangkai
	 * @param request
	 * @return
	 */
	private Map<String,Cookie> ReadCookieMap(HttpServletRequest request){  
	    Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();
	    Cookie[] cookies = request.getCookies();
	    if(null!=cookies){
	        for(Cookie cookie : cookies){
	            cookieMap.put(cookie.getName(), cookie);
	        }
	    }
	    return cookieMap;
	}
}
